/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 添加供应商界面
 * @Author：pinea
 */

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TSupplier;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.service.TSupplierService;

public class DataSupplierAddOrModifyDialog extends IDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent supplierID;
	private JLabelJTextFieldComponent name;
	private JLabelJTextAreaComponent location;
	private JLabelJTextFieldComponent mobile;
	private JLabelJTextFieldComponent phone;
	private JLabelJTextFieldComponent contact;
	private JLabelJTextAreaComponent note;
	private JLabelJComboBoxComponent IsValid;
	
	
	private JButton sureButton;
	private JButton cancelButton;
	private TSupplierService tSupplierService;//
	private TSupplier tSupplier = null;
	public boolean isSuccess=false;
	public DataSupplierAddOrModifyDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tSupplierService=BuildSpringApplication.getInstance().getTSupplierServiceImpl();
		this.setSize(350, 450);
		this.setLocationRelativeTo(null);
		this.init();
		this.initListener();
	}
	
	public void init() {
		JPanel main_jp = new BorderJPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setOpaque(true);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		supplierID= new JLabelJTextFieldComponent("  *", "编    号：", 30);
		name = new JLabelJTextFieldComponent("*", "供货商名称：", 30);
		contact = new JLabelJTextFieldComponent("  *", "联 系 人：", 30);
		mobile= new JLabelJTextFieldComponent("  *", "手    机：", 30);
		phone= new JLabelJTextFieldComponent("    固    话：", 30);
		IsValid= new JLabelJComboBoxComponent("    是否有效：",new String[]{"有效","无效"}, 187);
		JLabelJTextFieldComponent operator= new JLabelJTextFieldComponent("  *", "操 作 人：", 30);
		operator.getText().setEnabled(false);
		operator.getText().setText(ApplicationContext.OPERATOR_NAME);
		location = new JLabelJTextAreaComponent("    地    址：",  23,2);
		note = new JLabelJTextAreaComponent("    备    注：", 23,2);
		
		supplierID.getText().setText(UUID.GenUUID15());
		supplierID.getText().setEditable(false);
		
		
		JPanel p1 = new JPanel();
		sureButton=new RPJButtonWhite("保存(Ctrl+S)");
		cancelButton=new RPJButtonWhite("取消(Ctrl+Q)");
		p1.add(sureButton);
		p1.add(cancelButton);
		p1.setOpaque(false);
		
		main_jp.add(supplierID);
		main_jp.add(name);
		main_jp.add(contact);
		main_jp.add(mobile);
		main_jp.add(phone);
		main_jp.add(IsValid);
		main_jp.add(location);
		main_jp.add(note);
		main_jp.add(operator);
		main_jp.add(p1);
		this.add(main_jp);
		main_jp.setCursor(Util.CURSOR_DEFAULT);
	}
	
	@Override
	protected java.awt.Dimension getTitleSize() {
		return new Dimension(118, 29);
	};
	
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			String supplierIDStr=supplierID.getText().getText().trim();
			String nameStr = name.getText().getText().trim();
			String contactStr = contact.getText().getText().trim();
			String mobileStr= mobile.getText().getText().trim();
			String phoneStr=phone.getText().getText().trim();
			String IsValidStr= IsValid.getBox().getSelectedItem().toString();
			String locationStr = location.getArea().getText().trim();
			String noteStr = note.getArea().getText().trim();
			
			if(nameStr.equals("")||nameStr.length()>100){
				JOptionPane.showMessageDialog(null, "供货商名称不能为空，且不能大于100个字");
				name.getText().requestFocus();
				return;
			}
			if(contactStr.length()>20){
				JOptionPane.showMessageDialog(null, "联系人不能大于20个字");
				contact.getText().requestFocus();
				return;
			}
			if(mobileStr.length()>30){
				JOptionPane.showMessageDialog(null, "手机号码不能大于30个字");
				mobile.getText().requestFocus();
				return;
			}
			if(phoneStr.length()>30){
				JOptionPane.showMessageDialog(null, "固定电话不能大于30个字");
				phone.getText().requestFocus();
				return;
			}
			
			if(locationStr.length()>256){
				JOptionPane.showMessageDialog(null, "地址不能大于256个字");
				location.getArea().requestFocus();
				return;
			}
			if(noteStr.length()>512){
				JOptionPane.showMessageDialog(null, "备注不能大于512个字");
				note.getArea().requestFocus();
				return;
			}
			boolean isOpLog=false;
			if(tSupplier==null){
				tSupplier = new TSupplier();
				isOpLog=true;
			}
			tSupplier.setSupplierID(supplierIDStr);
			tSupplier.setName(nameStr);
			tSupplier.setContact(contactStr);
			tSupplier.setAddress(locationStr);
			tSupplier.setRemark(noteStr);
			tSupplier.setMobile(mobileStr);
			tSupplier.setPhone(phoneStr);
			tSupplier.setOperator(ApplicationContext.OPERATOR_ID);
			if(IsValidStr.equals("有效")){
				tSupplier.setIsValid(true);
			}else{
				tSupplier.setIsValid(false);
			}
			tSupplier.setHappenTime(new Date());
			try {
				if (isOpLog) {
					List<TSupplier> list = tSupplierService.findByNameService(nameStr);
					if (list != null && list.size() != 0) {
						if (Util.showConfirmDialog("存在相同名字的供货商，确定添加吗？") != 0)
							return;
					}
				}
				tSupplierService.saveOrUpdateService(tSupplier);
				this.isSuccess=true;
				if(isOpLog){
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加供应商信息");
				}else{
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改供应商信息");	
				}
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == cancelButton) {
			tSupplier = null;
			this.isSuccess=false;
			this.dispose();
		} else {
			super.actionPerformed(e);
		}
		
	}



	public void showDialog(TSupplier tSupplier){
		this.isSuccess=false;
		if(tSupplier!=null){
			this.tSupplier=tSupplier;
			supplierID.getText().setText(tSupplier.getSupplierID());
			name.getText().setText(tSupplier.getName());
			contact.getText().setText(tSupplier.getContact());
			mobile.getText().setText(tSupplier.getMobile());
			phone.getText().setText(tSupplier.getPhone());
			if(tSupplier.isIsValid()){
				IsValid.getBox().setSelectedItem("有效");
			}else{
				IsValid.getBox().setSelectedItem("无效");
			}
			location.getArea().setText(tSupplier.getAddress());
			note.getArea().setText(tSupplier.getRemark());
		}
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		DataSupplierAddOrModifyDialog dialog = new DataSupplierAddOrModifyDialog(null, "添加员工", true);
		dialog.setVisible(true);
	}
}
